
local jass = require 'jass.common'


register('钩锁长度',1500)
register('钩锁速度',700)
register('钩锁次数',{
    set = function(self)
        self:notify('单位-钩锁次数变动',self)
    end
})


local mt = ac.skill['钩子']
mt.art = [[ReplaceableTextures\CommandButtons\BTNImpale.blp]]
mt.model1 = [[Abilities\Weapons\WardenMissile\WardenMissile.mdl]]
mt.tip = [[对着海里钩可以获得好东西|n|n箱子=木材，鱼=金币]]
mt.targettype = '点'
mt.is_order = 1
mt.range = 9999
mt.cool = 0.01
mt.resume_flag = true

function mt:show_range()
    local hero = self.owner
    return hero:get('钩锁长度')
end

function mt:on_add()
    local hero = self.owner
    self:set_enable(false)
    self:gc(hero:event '单位-钩锁次数变动'(function()
        self.show_num = math.floor(hero:get('钩锁次数'))
        if self.show_num==0 then
            if self:is_enable() then
                self:set_enable(false)
            end
        else
            if not self:is_enable() then
                self:set_enable(true)
            end
        end
        self.show_effect = self.show_num>0
    end))
    
end


function mt:get_base_item(point)
    local target = ac.player[16]:create_unit('一条小鱼',point)
    target:add_restriction '无敌'
    return target
end

function mt:on_cast_shot()
    local hero = self.owner
    local player = hero.owner
    local point1 = hero:get_point()
    local point2 = self.target:get_point()
    local angle = point1 / point2
    local count = 0
    local range = hero:get('钩锁长度')
    local speed = hero:get('钩锁速度')
    local distance = 0.01 * speed
    if hero:get('钩锁次数')<=0 then
        return
    end
    hero:add('钩锁次数',-1)
    local data = {}
    data.effect = {}
    data.angle = angle
    
    data.effect1 = point1:effect
    {
        model = hero:get_model(),
        angle = angle,
        hide_when_remove = true,
        size = hero:get_size(),
        alpha = 50,
    }
    ac.loop(10,function(t)
        count = count + 1
        range = range - distance
        local point = point1 - {angle,count * distance}
        local in_map = ac.rect.map:has(point)
        if data.hanger==nil then
            data.hanger = hero:create_unit('钩子-头',point,angle)
        end
        if range%35<=distance and in_map then
            local effect = hero:create_unit('钩子',point,angle)
            data.hanger:set_point(point)
            effect:set_point(point)
            -- local effect = point:effect
            -- {
            --     model1 = self.model1,
            --     height = 60,
            --     size = 1.5,
            --     angle = angle,
            -- }
            table.insert(data.effect,effect)
        end
        local unit = ac.selector('unit'): range(point - {angle,50},35):can_god():player(ac.player[16]):filter(function(u)
            return u:get_data('勾中')==nil
        end):first()
        if unit then
            t:remove()
            self:notify('on_run',unit,data)
            return
        end
        if range<=0 or in_map==false then
            local x,y = point:get()
            local target = nil
            t:remove()
            if jass.IsTerrainPathable( x,y, 6)==false then
                target = self:get_base_item(point)
            else
                player:send_msg('你钩子钩了一坨屎')
            end
            self:notify('on_run',target,data)
            return
        end
    end)
end

local need_tip = {
    ['超级大鱼'] = true,
    ['深海巨鲸'] = true,
    ['泰坦鱼'] = true,
    ['珍藏宝箱'] = true,
}

function mt:on_run(target,data)
    local hero = self.owner
    local effects = data.effect
    --没有钩中猎物
    if not target then
        target = ac.player[16]:create_unit('一条小鱼',effects[#effects])
        target:add_restriction '无敌'
    end
    if target then
        target.player = hero.owner
        target:add_restriction '硬直'
        target:set_data('勾中',true)
    end
    ac.loop(30,function(t)
        local effect = table.remove(effects,#effects)
        if effect then
            local point = effect:get_point()
            data.hanger:set_point(point)
            target:set_point(point - {data.angle,50})
            effect:remove()
        else
            if data.hanger then
                data.hanger:remove()
            end
            
            data.effect1:remove()
            hero:issue_order('attack',hero:get_point())
            t:remove()
            if target then
                target:remove_restriction '硬直'
                if target:has_restriction '无敌' then
                    target:remove_restriction '无敌'
                end
                target:set_data('勾中',nil)
                if need_tip[target.name] then
                    barrage(hero.owner,('掉到一条|cffff9900%s|r'):format(target.name))
                end
                if ac.lni('item',target.name) then
                    target:remove()
                    local card = target.card
                    local data = {}
                    if card then
                        data.title = data.name
                        data.art = card.art
                    end
                    local item = hero:add_item(target.name,data)
                    item.card = card
                else
                    target:add_buff '中立怪进入战斗'{}
                end
                
            end
        end
    end)
end
